package com.ruoyi.projects.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.ruoyi.common.constant.BuConstants;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.projects.Vos.AppointInfo;
import com.ruoyi.projects.Vos.TimeArr;
import com.ruoyi.projects.Vos.slApointVo;
import com.ruoyi.projects.domain.SlCustom;
import com.ruoyi.projects.mapper.SlCustomMapper;
import com.ruoyi.projects.mapper.SlProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.projects.mapper.SlApointFormMapper;
import com.ruoyi.projects.domain.SlApointForm;
import com.ruoyi.projects.service.ISlApointFormService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 预约排期Service业务层处理
 *
 * @author yangz
 * @date 2024-10-27
 */
@Service
public class SlApointFormServiceImpl implements ISlApointFormService {
    @Autowired
    private SlApointFormMapper slApointFormMapper;

    @Autowired
    private SlProductMapper slProductMapper;

    @Autowired
    private SlCustomMapper customMapper;

    // 声明一个List集合，并初始化放入一些元素，这里以存放String类型对象为例
    public static List<TimeArr> timeArrs = new ArrayList<>();

    static {
        // 在静态代码块中向集合添加初始元素
        timeArrs.add(new TimeArr("9:00", "11:00"));
        timeArrs.add(new TimeArr("11:00", "13:00"));
        timeArrs.add(new TimeArr("13:00", "15:00"));
        timeArrs.add(new TimeArr("15:00", "17:00"));
        timeArrs.add(new TimeArr("17:00", "19:00"));
        timeArrs.add(new TimeArr("19:00", "21:00"));
    }

    /**
     * 查询预约排期
     *
     * @param apointId 预约排期主键
     * @return 预约排期
     */
    @Override
    public SlApointForm selectSlApointFormByApointId(Long apointId) {
        return slApointFormMapper.selectSlApointFormByApointId(apointId);
    }

    /**
     * 查询预约排期列表
     *
     * @param slApointForm 预约排期
     * @return 预约排期
     */
    @Override
    public List<SlApointForm> selectSlApointFormList(SlApointForm slApointForm) {
        return slApointFormMapper.selectSlApointFormList(slApointForm);
    }

    /**
     * 新增预约排期
     *
     * @param slApointForm 预约排期
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSlApointForm(SlApointForm slApointForm) {
        slApointForm.setCreateTime(DateUtils.getNowDate());
        return slApointFormMapper.insertSlApointForm(slApointForm);
    }

    /**
     * 修改预约排期
     *
     * @param slApointForm 预约排期
     * @return 结果
     */
    @Override
    public int updateSlApointForm(SlApointForm slApointForm) {
        slApointForm.setUpdateTime(DateUtils.getNowDate());
        return slApointFormMapper.updateSlApointForm(slApointForm);
    }

    /**
     * 批量删除预约排期
     *
     * @param apointIds 需要删除的预约排期主键
     * @return 结果
     */
    @Override
    public int deleteSlApointFormByApointIds(Long[] apointIds) {
        return slApointFormMapper.deleteSlApointFormByApointIds(apointIds);
    }

    /**
     * 删除预约排期信息
     *
     * @param apointId 预约排期主键
     * @return 结果
     */
    @Override
    public int deleteSlApointFormByApointId(Long apointId) {
        return slApointFormMapper.deleteSlApointFormByApointId(apointId);
    }

    @Override
    public List<TimeArr> getApointFormInfo(List<slApointVo> slApointVos) {
        if (slApointVos == null) {
            return timeArrs;
        } else {
            List<TimeArr> todayTimeArrs=new ArrayList<>();
            timeArrs.forEach(time -> {
                TimeArr timeArr = new TimeArr(time.getStartHour(), time.getEndHour());
                slApointVos.forEach(item ->{
                    String[] split = item.getTimeSolt().split("-");
                    if (isOverlap(split[0],split[1],timeArr)){
                        timeArr.setIsBooked(true);
                        timeArr.setAppointInfo(packageInfo(item));
                    }
                });
                todayTimeArrs.add(timeArr);
            });
            return todayTimeArrs;
        }
    }
    //组装预约时间段信息的工具方法
    public static AppointInfo packageInfo(slApointVo slApointVo){
        AppointInfo appointInfo = new AppointInfo();
        appointInfo.setApointType(slApointVo.getType());
        appointInfo.setAppointId(slApointVo.getId());
        appointInfo.setCustomerName(slApointVo.getCustomerName());
        appointInfo.setPhone(slApointVo.getCustomerPhone());
        appointInfo.setProductNames(slApointVo.getServiceName().split(","));
        return appointInfo;
    }
    public static boolean isOverlap(String startTime,String endTime, TimeArr time2) {
        // 比较开始时间和结束时间来判断是否重合
        return!(endTime.compareTo(time2.getStartHour()) < 0 ||
                time2.getEndHour().compareTo(startTime) < 0);
    }

    /**
     * 查看某个产品某个会员的预约记录
     *
     * @param vo
     * @return
     */
    @Override
    public List<String> queryAppointMents(SlApointForm vo) {
        List<SlApointForm> slApointForms = slApointFormMapper.selectSlApointFormList(vo);
        if (slApointForms != null && slApointForms.size() > 0) {
            List<String> timesTrs = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            slApointForms.forEach(item -> {
                String startTime = sdf.format(item.getStartHour());
                String endTime = sdf.format(item.getEndHour());
                timesTrs.add(startTime + "-" + endTime);
            });
            return timesTrs;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 查询日历预约记录
     *
     * @param slApointForm
     * @return
     */
    @Override
    public List<slApointVo> queryCalendarApoints(SlApointForm slApointForm) {
        List<SlApointForm> slApointForms = slApointFormMapper.selectSlApointFormList(slApointForm);
        if (slApointForms != null && slApointForms.size() > 0) {
            List<slApointVo> slApointVos = new ArrayList<>();
            slApointForms.forEach(item -> {
                slApointVo slApointVo = new slApointVo();
                slApointVo.setCreateTime(item.getCreateTime());
                slApointVo.setId(item.getApointId());
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                String startTime = sdf.format(item.getStartHour());
                String endTime = sdf.format(item.getEndHour());
                slApointVo.setTimeSolt(startTime + "-" + endTime);
                Integer ifOrder = item.getIfOrder();
                //预约状态pending-待服务, completed-已完成, cancelled-已取消
                if (BuConstants.APOINT_ORDER_STATE_WAIT.equals(ifOrder)) {
                    //待核销
                    slApointVo.setStatus("pending");
                    slApointVo.setStatusText("待服务");
                } else if (BuConstants.APOINT_ORDER_STATE_DONE.equals(ifOrder)) {
                    //已完成
                    slApointVo.setStatus("completed");
                    slApointVo.setStatusText("已完成");
                } else {
                    //已取消
                    slApointVo.setStatus("cancelled");
                    slApointVo.setStatusText("已取消");
                }
                slApointVo.setServiceName(slProductMapper.queryProductNames(item.getProductId()));
                SlCustom slCustom = customMapper.selectSlCustomById(item.getCustomId());
                if (slCustom != null) {
                    slApointVo.setCustomerName(slCustom.getNickname());
                    slApointVo.setCustomerPhone(slCustom.getPhone());
                }
                slApointVo.setType(item.getApointType().toString());
                slApointVos.add(slApointVo);
            });
            return slApointVos;
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public List<slApointVo> querySlApointVo(SlApointForm slApointForm) {
        List<SlApointForm> slApointForms = slApointFormMapper.selectSlApointFormList(slApointForm);
        if (slApointForms != null && slApointForms.size() > 0) {
            List<slApointVo> slApointVos = new ArrayList<>();
            slApointForms.forEach(item -> {
                slApointVo slApointVo = new slApointVo();
                slApointVo.setCreateTime(item.getCreateTime());
                slApointVo.setId(item.getApointId());
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                String startTime = sdf.format(item.getStartHour());
                String endTime = sdf.format(item.getEndHour());
                slApointVo.setTimeSolt(startTime + "-" + endTime);
                Integer ifOrder = item.getIfOrder();
                //预约状态pending-待服务, completed-已完成, cancelled-已取消
                if (BuConstants.APOINT_ORDER_STATE_WAIT.equals(ifOrder)) {
                    //待核销
                    slApointVo.setStatus("pending");
                    slApointVo.setStatusText("待服务");
                } else if (BuConstants.APOINT_ORDER_STATE_DONE.equals(ifOrder)) {
                    //已完成
                    slApointVo.setStatus("completed");
                    slApointVo.setStatusText("已完成");
                } else {
                    //已取消
                    slApointVo.setStatus("cancelled");
                    slApointVo.setStatusText("已取消");
                }
                slApointVo.setServiceName(slProductMapper.queryProductNames(item.getProductId()));
                SlCustom slCustom = customMapper.selectSlCustomById(item.getCustomId());
                if (slCustom != null) {
                    slApointVo.setCustomerName(slCustom.getNickname());
                    slApointVo.setCustomerPhone(slCustom.getPhone());
                }
                slApointVo.setType(item.getApointType().toString());
                slApointVos.add(slApointVo);
            });
            return slApointVos;
        } else {
            return new ArrayList<>();
        }
    }

}
